﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Logic.BusinessLogic.SocialNetworks;
using System.Web.Services;
using SocialNetworks.Config;

public partial class Pages_Dashboard_Account_Video : System.Web.UI.Page, ICrumbedPage
{

	public int AccountID
	{
		get
		{
			int temp;
			int.TryParse(Request["ID"], out temp);
			return temp;
		}
	}

    protected void Page_Load(object sender, EventArgs e)
    {
		//
		if (!IsPostBack)
			LoadData();
    }

	private void LoadData()
	{
		FillDropDownlist();
		LoadRecentPosts();
        ucAccountSideMenu.AccountID = AccountID;
	}

	private void LoadRecentPosts()
	{
		var posts = Logic.BusinessLogic.Stats.GetPostsByAccountID(AccountID, BO.Post.PostTypes.Video,5);
		repPosts.DataSource = posts;
		repPosts.DataBind();
	}

	private void FillDropDownlist()
	{
		try
		{
			var myXpathdocument = new System.Xml.XPath.XPathDocument("http://gdata.youtube.com/schemas/2007/categories.cat");
			var xPathNavigator = myXpathdocument.CreateNavigator();
			var myNS = new System.Xml.XmlNamespaceManager(xPathNavigator.NameTable);
			myNS.AddNamespace("atom", "http://www.w3.org/2005/Atom");
			myNS.AddNamespace("yt", "http://gdata.youtube.com/schemas/2007");
			var myNodeIterator = xPathNavigator.Select("//atom:category[yt:assignable]/@term", myNS);

			ddlCategory.DataSource = myNodeIterator;

			ddlCategory.DataTextField = "value";
			ddlCategory.DataValueField = "value";
			ddlCategory.DataBind();
		}
		catch (Exception)
		{
			//fixme: need to do something else instead , when we get here.
		}

	}


	//protected void btnPost_Click(object sender, EventArgs e)
	//{
	//    var token = CreateVideo(AccountID, tbTitle.Text, ddlCategory.SelectedValue, tbKeywords.Text, tbDescription.Text, true, true);
	//    string script = string.Format("uploader.submitForm({0},{1});", token.Token, token.Url);
	//    ScriptManager.RegisterStartupScript(this,typeof(Page),"open",script, true );
	//}


	public class TokenResponse
	{
		public string Url;
		public string Token;
	}
	public TokenResponse CreateVideo(int accountID, string title, string category, string keywords, string description, bool isFacebook, bool isTwitter)
	{
		TokenResponse response = new TokenResponse();
		BO.User user = Authentication.GetCurrentUser();
		BO.Account account = BLL.Accounts.GetByID(accountID);
		//TODO:Authenticate

		var api = new Social.YouTube.YouTubeApi(new YouTubeConfig { SessionToken = account.YtToken });
		var token = api.GetFormUploadToken(title, category, keywords, description);

		var post = Logic.BusinessLogic.Posting.InsertVideoToDB(title, description, user.ID, accountID, snSelector.GetSelector());

		response.Url = string.Format("{0}?{1}", token.Url,AppParams.URLs.YoutubeUploaderNextUrl(post.ID.ToString()));
		response.Token = token.Token;

		return response;
	}




    #region ICrumbedPage Members

    public int Level
    {
        get { return 4; }
    }

    public string TailName { get { return "Upload video"; } }

    #endregion
}